//
//  YQLocationShowCityCell.swift
//  MuYing
//
//  Created by tbb on 2019/8/21.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit

@objc protocol YQLocationShowCityCellDelegate:NSObjectProtocol {
    
    func didSelectItemAtImageAction(_ indexPath:NSIndexPath,_ item:NSInteger);
    
}

class YQLocationShowCityCell: YQTableViewBaseCell,UICollectionViewDelegate,UICollectionViewDataSource {
    @objc weak var delegate: YQLocationShowCityCellDelegate?
    var cityModels:[String] = ["广州","深圳","汕头","湛江","茂名","汕尾"];
    @objc var selectIndexPath:NSIndexPath?;
    lazy var cityCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        let w = ((kSwiftkScreenSizeWidth - 88) - (33 + 14 + 8)) / 3.0;
        layout.itemSize = CGSize(width: w, height: 20);
        layout.minimumLineSpacing = 12.0;
        layout.minimumInteritemSpacing = 4.0;
        let cityCollView = UICollectionView(frame: CGRect(x: 33, y: 0, width: (kSwiftkScreenSizeWidth - 88) - (33 + 14), height: 50), collectionViewLayout: layout);
        cityCollView.backgroundColor = UIColorFromHex(rgbValue: 0xF6F6F6);
        cityCollView.delegate = self;
        cityCollView.dataSource = self;
        cityCollView.bounces = false;
        cityCollView.showsVerticalScrollIndicator = false;
        cityCollView.register(UINib.init(nibName: "YQLocationShowCityCollCell", bundle: nil), forCellWithReuseIdentifier: "YQLocationShowCityCollCell");
        return cityCollView;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.contentView.backgroundColor = UIColorFromHex(rgbValue: 0xF6F6F6);
        self.contentView.addSubview(self.cityCollView);
        self.cityCollView.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(33);
            make?.top.mas_equalTo()(0);
            make?.right.mas_equalTo()(-14);
            make?.bottom.mas_equalTo()(-20);
        }
    }

}

extension YQLocationShowCityCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.cityModels.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YQLocationShowCityCollCell", for: indexPath) as? YQLocationShowCityCollCell else {
            return UICollectionViewCell();
        }
        cell.titleLb.text = self.cityModels[indexPath.item];
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? YQLocationShowCityCollCell else {
            return;
        }
        cell.isSelectCell = true;
        
        
        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectItemAtImageAction::"))), let selectIndexPath = self.selectIndexPath {
            self.delegate?.didSelectItemAtImageAction(selectIndexPath,indexPath.item);
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? YQLocationShowCityCollCell else {
            return;
        }
        cell.isSelectCell = false;
    }
}
